package com.shop.loan.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.loan.mapper.AppByMybatisMapper;
import com.shop.loan.mapper.NoticeMybatisMapper;
import com.shop.loan.mapper.SettingByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.AppByMybatis;
import com.shop.loan.model.entity.LoanOrderDeviceInfoByMybatis;
import com.shop.loan.model.entity.SettingByMybatis;
import com.shop.loan.model.enums.AppType;
import com.shop.loan.model.vo.ChannelOpenVo;
import com.shop.loan.model.vo.HomeResVo;
import com.shop.loan.model.vo.UserLoginVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.SettingService;
import com.shop.loan.util.PasswordUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class SettingServiceImpl implements SettingService {

    public static Long settingId = 1L;

    @Resource
    RedisCache redisCache;

    @Resource
    RedisUtil redisUtil;

    @Resource
    NoticeMybatisMapper noticeMybatisMapper;

    @Resource
    SettingByMybatisMapper settingByMybatisMapper;

    @Resource
    AppByMybatisMapper appByMybatisMapper;

    @Override
    public SettingByMybatis getSetting() {
        return redisCache.getSetting(false);
    }

    @Override
    public ResResult home(UserLoginVo userLoginVo) {
        HomeResVo homeResVo = new HomeResVo();

        SettingByMybatis setting = getSetting();
        JSONObject settingJs = JSON.parseObject(JSON.toJSONString(setting));
        settingJs.remove("autoAllocLoanService");
        settingJs.remove("autoAllotAfterService");
        settingJs.remove("autoAllotCollectionService");
        settingJs.remove("machineAuto");
        settingJs.remove("machineAutoBlack");
        settingJs.remove("payPassword");
        settingJs.remove("loanPassword");
        settingJs.remove("msgNotifyHour");
        settingJs.remove("withholdHourByOverdue");
        settingJs.remove("updateUserId");
        settingJs.remove("createUserId");
        settingJs.remove("withholdExpire");
        settingJs.remove("reportTime");
        settingJs.remove("autoLoan");
        settingJs.remove("withholdHour");
        settingJs.remove("machineAutoReport");
        settingJs.remove("withholdOverdue");
        settingJs.remove("channelBlack");
        settingJs.remove("endHideOrder");

        homeResVo.setSetting(settingJs);
        AppByMybatis app_a = appByMybatisMapper.selectByType(AppType.android.getType());
        if(app_a != null){
            homeResVo.setAndroidUrl(app_a.getUrl());
        }
        homeResVo.setNotices(noticeMybatisMapper.selectByList());
        return new ResResult(Code.SUCCESS, homeResVo);
    }


    @Override
    public ResResult open(ChannelOpenVo channelOpenVo) {
        if (!StringUtils.hasText(channelOpenVo.getCode())) {
            return new ResResult(Code.PARAM_LACK);
        }
        redisUtil.incr(RedisCache.channelPageOpenPv + channelOpenVo.getCode() + "_" + DateUtil.today());

        if (StringUtils.hasText(channelOpenVo.getDeviceInfo()) && JSON.isValidObject(channelOpenVo.getDeviceInfo())) {
            LoanOrderDeviceInfoByMybatis loanOrderDeviceInfoByMybatis = JSON.parseObject(channelOpenVo.getDeviceInfo(), LoanOrderDeviceInfoByMybatis.class);
            if (StringUtils.hasText(loanOrderDeviceInfoByMybatis.getDeviceId())) {
                redisUtil.hset(RedisCache.channelPageOpenUv + channelOpenVo.getCode() + "_" + DateUtil.today(), loanOrderDeviceInfoByMybatis.getDeviceId(), "1");
            }
        }
        return new ResResult(Code.SUCCESS);
    }


    @Override
    public ResResult updateLoanPassword(String password) {
        if(!StringUtils.hasText(password)){
            return new ResResult(Code.PARAM_INVALID);
        }
        SettingByMybatis setting = getSetting();
        setting.setId(settingId);
        setting.setLoanPassword(PasswordUtil.getEncrypt(password));
        settingByMybatisMapper.updateByPrimaryKeySelective(setting);

        redisUtil.del(RedisCache.settingCache);
        return new ResResult(Code.SUCCESS);
    }

    @Override
    public ResResult updatePayPassword(String password) {
        if(!StringUtils.hasText(password)){
            return new ResResult(Code.PARAM_INVALID);
        }
        SettingByMybatis setting = getSetting();
        setting.setId(settingId);
        setting.setPayPassword(PasswordUtil.getEncrypt(password));
        settingByMybatisMapper.updateByPrimaryKeySelective(setting);

        redisUtil.del(RedisCache.settingCache);
        return new ResResult(Code.SUCCESS);
    }
}
